﻿using System;
using System.Collections.Generic;
using Elasticsearch.Synchronize.Domain.Events;
using Elasticsearch.Synchronize.Domain.SeedWork;
using Elasticsearch.Synchronize.Common.Enum;
using MediatR;

namespace Elasticsearch.Synchronize.Domain.Aggregates.SynchronizeAggregate
{
    public class SynchronizeItem : Entity, IAggregateRoot
    {
        public SynchronizeItem(IMediator mediator = null) : base(mediator)
        {
        }

        public string Identity { get; set; }

        public string RuleId { get; set; }

        public string MongoSeverUrl { get; set; }

        public string MongoDatabase { get; set; }

        public string MongoCollection { get; set; }

        public string JsonFilter { get; set; }

        public DataSource Source { get; set; }

        public string ElasticsearchServerUrl { get; set; }

        public string ElasticsearchIndexName { get; set; }

        public string ElasticsearchTypeName { get; set; }

        public int Threshold { get; set; }

        public SyncState State { get; set; }

        public bool Enabled { get; set; }

        //public enum SyncState
        //{
        //    Idle = 0,
        //    Pouring = 1,
        //    Tail = 2,
        //    Stopped = 3
        //}

        //public enum DataSource
        //{
        //    HexinDbEventJoin = 0,
        //    ScarmDbContactsContact = 1
        //}

        public override bool IsTransient()
        {
            throw new NotImplementedException();
        }

        public SynchronizeItem Merge(SynchronizeItem item)
        {
            this.Identity = item.Identity;
            this.RuleId = item.RuleId;
            this.MongoSeverUrl = item.MongoSeverUrl;
            this.MongoDatabase = item.MongoDatabase;
            this.MongoCollection = item.MongoCollection;
            this.JsonFilter = item.JsonFilter;
            this.Source = item.Source;
            this.ElasticsearchServerUrl = item.ElasticsearchServerUrl;
            this.ElasticsearchIndexName = item.ElasticsearchIndexName;
            this.ElasticsearchTypeName = item.ElasticsearchTypeName;
            this.Threshold = item.Threshold;
            this.State = item.State;
            this.Enabled = item.Enabled;

            return this;
        }

        public void Scan()
        {
            Console.WriteLine("SynchronizeItem.Scan");
            base.DomainEventPublish(new ScanSynchronizeItemDomainEvent(this, DateTime.Now));
        }

        public void Pour()
        {
            Console.WriteLine("SynchronizeItem.Pour");
            base.DomainEventPublish(new PourDataToElasticsearchDomainEvent(this, DateTime.Now));
        }
    }
}